/*
   Copyright (c) 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2015-01-11 Marco Riccardi - initial implementation
 *  
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLAttribute;
import org.mizartools.utility.xml.XMLElement;

public class FlexFrm extends Formula implements IElement {

	public static final String NAME = "FlexFrm";

    private INode parentNode;

    private LinkedList<Formula> formulaList = new LinkedList<Formula>();
    private LinkedList<Term> termList = new LinkedList<Term>();

    private Formula formula;

    private FlexFrm(INode parentNode){
        this.parentNode = parentNode;
    }

    public INode getParentNode() {
        return parentNode;
    }

    public static FlexFrm newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        FlexFrm flexFrm = new FlexFrm(parentNode);
        XMLElement xmlElement = xmlElementList.poll();
        if (!xmlElement.getName().equals(NAME)){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a " + NAME, xmlElementList);
        }
        for (XMLAttribute xmlAttribute : xmlElement.getXMLAttributeList()){
        	throw new ElementParseException(ElementParseException.NOT_VALID_ATTRIBUTE_ELEMENT, "XMLElement has not a valid attribute " + xmlAttribute.getName(), xmlElementList);
        }

        if (!xmlElement.isEmpty()){
            while (true){
                xmlElement = xmlElementList.peek();
                if (xmlElement == null){
                    throw new ElementParseException(ElementParseException.UNEXPECTED_END_OF_FILE, "Unexpected end of XMLElement list");
                }
                if (xmlElement.getName().equals(NAME) && xmlElement.isEndElement()){
                    xmlElement = xmlElementList.poll();
                    break;
                }
                if (Formula.isFormula(xmlElement.getName()) && flexFrm.termList.size() == 0){
                    flexFrm.formulaList.add(Formula.newInstance(flexFrm, xmlElementList));
                } else if (Term.isTerm(xmlElement.getName()) && flexFrm.formula == null){
                    flexFrm.termList.add(Term.newInstance(flexFrm, xmlElementList));
                } else if (Formula.isFormula(xmlElement.getName())){
                    if (flexFrm.formula == null) {
                    	flexFrm.formula = Formula.newInstance(flexFrm, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Formula> in element " + NAME, xmlElementList);
                    }
                }
                else 
                    throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "The XMLElement " + xmlElement.getName() + " is not valid in " + NAME, xmlElementList);
            }
        }

        return flexFrm;
    }

    public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        XMLElement xmlElement = new XMLElement(NAME);
        boolean isEmpty = !(!formulaList.isEmpty() || !termList.isEmpty() || formula != null);
        xmlElement.setEmpty(isEmpty);
        xmlElementList.add(xmlElement);
        for (Formula formula : this.formulaList){
            xmlElementList.addAll(formula.getXMLElementList());
        }
        for (Term term : this.termList){
            xmlElementList.addAll(term.getXMLElementList());
        }
        if (this.formula != null) {
        	xmlElementList.addAll(formula.getXMLElementList());
        }
        if (!isEmpty) { 
            xmlElement = new XMLElement(NAME);
            xmlElement.setEndElement(true);
            xmlElementList.add(xmlElement);
        }
        return xmlElementList;
    }

    public LinkedList<Formula> getFormulaList(){
        return formulaList;
    }

    public LinkedList<Term> getTermList(){
        return termList;
    }
    
	@Override
	public void accept(Visitor visitor) {
		if (visitor.visit(this)) {
			for(Formula formula: this.formulaList) {
				formula.accept(visitor);
			}
			for(Term term: this.termList) {
				term.accept(visitor);
			}
		}
		visitor.endVisit(this);
	}

	@Override
	public String toString(){
		ToStringVisitor toStringVisitor = new ToStringVisitor();
		this.accept(toStringVisitor);
		return toStringVisitor.toString();
	}

}
